const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    FjLoginLog: '',
    openInfo: {},
    // 回收区域
    areaList: [],
    // 物资列表
    goodsList: [],
    // 消息提醒免打扰
    remindingChecked: false,
    appChecked: false,
    weChatChecked: false,
    msgChecked: false,
    aiChecked: false,
    manChecked: false,

    // 服务通知账号管理
    authorizationFlag: true,
    accountList: [],

    // 关闭服务弹窗show
    show: false,
    preStatus: 1
  },
  // 添加回收区域
  addArea(){
    wx.navigateTo({
      url: '/packageB/pages/recyclingPreference/recyclingArea'
    })
  },
  toEditArea(){
    wx.navigateTo({
      url: '/packageB/pages/recyclingPreference/recyclingArea'
    })
  },
  delArea(e){
    var _this = this
    wx.request({
      url: app.data.api_url2+"v/sign_zone_del/",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: {
        'user_id': this.data.FjLoginLog.UID,
        'id': e.target.dataset._id,
      },
      method: 'POST',
      success: function(res) {   
        if (res.statusCode == 200) {
          if(res.data.code == 200){
            _this.getAreaList()
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      }
    }) 
  },
  // 添加回收物资
  addGoods(){
    wx.navigateTo({
      url: '/packageB/pages/recyclingPreference/recyclingGoods'
    })
  },
  editGoods(e){
    wx.navigateTo({
      url: `/packageB/pages/recyclingPreference/recyclingGoods?_id=${e.target.dataset._id}`,
    })
  },
  delGoods(e){
    var _this = this
    wx.request({
      url: app.data.api_url2+"v/vip_cate_del/",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: {
        'id': e.target.dataset._id,
      },
      method: 'POST',
      success: function(res) {   
        if (res.statusCode == 200) {
          if(res.data.code == 200){
            _this.getGoodsList()
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      }
    }) 
  },
  // 消息免打扰
  remindingChange(e){
    this.setData({
      remindingChecked: e.detail.value
    })
    if(!e.detail.value){
      this.setData({
        appChecked: false,
        weChatChecked: false,
        msgChecked: false,
        aiChecked: false,
        manChecked: false,
      })
    } else {
      this.setData({
        aiChecked: true,
        manChecked: true,
      })
    }
    this.saveRemind()
  },
  switchChange(e){
    const obj = {}
    obj[`${e.target.dataset.type}Checked`] = e.detail.value
    this.setData({...this.data, ...obj})
    if(
      !this.data.appChecked &&
      !this.data.weChatChecked &&
      !this.data.msgChecked &&
      !this.data.aiChecked &&
      !this.data.manChecked 
    ) {
      this.setData({remindingChecked: false})
    }
    this.saveRemind()
  },
  // 保存提醒方式
  saveRemind(){
    var _this = this
    const choice = this.setParams()
    wx.request({
      url: app.data.api_url2+"v/vip_alarm_set/",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: {
        user_id: this.data.FjLoginLog.UID,
        sts: this.data.remindingChecked ? 1 : 0,
        choice
      },
      method: 'POST',
      success: function(res) {   
        if (res.statusCode == 200) {
          if(res.data.code !== 200){
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      }
    }) 
  },
  setParams(){
    const { appChecked, weChatChecked, msgChecked, aiChecked, manChecked } = this.data
    const arr = Array.from(
      [appChecked, weChatChecked, msgChecked, aiChecked, manChecked], 
      (item,index) => item && index + 1
    )
    return arr.filter(item => item)
  },
  // 服务通知账号管理
  // 去授权 
  toImpower(){
    wx.navigateTo({
      url: '/packageB/pages/recyclingPreference/authorization'
    })
  },

  // 编辑
  editAccount(e){
    const info = this.data.accountList.find(ite => ite.OpenID === e.target.dataset._openid)
    wx.navigateTo({
      url: '/packageB/pages/recyclingPreference/authorization',
      events: {},
      success: function(res) {
        res.eventChannel.emit('accountInfo', { data: info })
      }
    })
  },
  // 删除
  delAccount(e){
    var _this = this
    const url = "api/FeijiuApp/Feijiu_HeZuo_WxUserDEL"
    const params = {
      openid: e.target.dataset._openid,
      LoginUID: this.data.FjLoginLog.UID 
    }
    app.getDaoPostN(url, params, function(r){
      if(r.code == 200){
        _this.getAccountList()
      }else{
        wx.showToast({
          title: r.message,
          icon:'none'
        })
      }
    })
  },

  // 关闭服务
  closeServer(){
    if(this.data.preStatus == 0) {
      this.sure()
    }else {
      this.setData({
        show: true
      })
    }
  },
  cancel(){
    this.setData({
      show: false
    })
  },

  feedback(){
    wx.navigateTo({
      url: '/packageB/pages/assetsDisposal/CSExecutive'
    })
  },

  sure(){
    var _this = this
    wx.request({
      url: app.data.api_url2+"v/vip_close_or_open/",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: {
        'user_id': this.data.FjLoginLog.UID,
        'sts': this.data.preStatus == 1 ? 0 : 1
      },
      method: 'POST',
      success: function(res) {   
        if (res.statusCode == 200) {
          if(res.data.code == 200){
            _this.setData({
              show: false,
            })
            _this.getPreStatus()
            // wx.switchTab({
            //   url: '/pages/index/index',
            // })
            wx.showToast({
              title: '操作成功',
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      }
    }) 
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      this.setData({
        FjLoginLog : wx.getStorageSync('FjLoginLog')
      })
      this.getAreaList()
      this.getGoodsList()
      this.getRemindInfo()
      // 获取openId，然后调取服务列表进行逻辑操作
      this.getOpenId()
      this.getPreStatus()
     
      console.log('输出UID-----', this.data.FjLoginLog)
  },
  // 获取回收区域列表
  getAreaList(){
    var _this = this
    wx.request({
      // app.data.api_url2
      url:app.data.api_url2 +"v/sign_zone_list/",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: {
        'user_id': this.data.FjLoginLog.UID,
      },
      method: 'POST',
      success: function(res) {   
        if (res.statusCode == 200) {
          if(res.data.code == 200){
            _this.setData({
              areaList: res.data.data
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      }
    }) 
  },
  // 获取回收物资列表
  getGoodsList(){
    var _this = this
    wx.request({
      url: app.data.api_url2+"v/sign_cate_list/",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: {
        'user_id': this.data.FjLoginLog.UID,
      },
      method: 'POST',
      success: function(res) {   
        if (res.statusCode == 200) {
          if(res.data.code == 200){
            _this.setData({
              goodsList: res.data.data
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      }
    }) 
  },
  // 获取消息提醒
  getRemindInfo(){
    var _this = this
    wx.request({
      url: app.data.api_url2+"v/sign_alarm_info/",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: {
        'user_id': this.data.FjLoginLog.UID,
      },
      method: 'POST',
      success: function(res) {   
        if (res.statusCode == 200) {
          if(res.data.code == 200){
            const { sts, choice } = res.data.data
            if(sts == 1 && choice){
              const choiceArr = choice.split(',')
              let appChecked = choiceArr.includes('1')
              let weChatChecked = choiceArr.includes('2')
              let msgChecked = choiceArr.includes('3')
              let aiChecked = choiceArr.includes('4')
              let manChecked = choiceArr.includes('5')
              _this.setData({
                remindingChecked: true,
                appChecked,
                weChatChecked,
                msgChecked,
                aiChecked,
                manChecked,
              })
            }
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      }
    }) 
  },
  // 客户端无法从缓存中拿到openid
  getOpenId(){
    const _this = this
    wx.login({
      success: (res) => {
        if (res.code) {
          var api_input = {
            code: res.code
          }
          app.getDaoPostN('api/FeijiuApp/Feijiu_XCX_GetCode', api_input, function (r) {
            if (r.code == 200) {
              _this.setData({
                openInfo: JSON.parse(r.data)
              })
              _this.getAccountList()
            } else {
              wx.showToast({
                title: '网络延迟较高，请检测网络后再试。',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  // 获取接收账号
  getAccountList(){
    var that = this
    const url = "api/FeijiuApp/Feijiu_HeZuo_WxUserList"
    const params = { LoginUID: this.data.FjLoginLog.UID }
    app.getDaoPostN(url, params, function(r){
      if(r.code == 200){
        that.setData({
          accountList: r.data
        })
        if(r.data.length >= 3){
          that.setData({
            authorizationFlag: false 
          })
        } else {
          const aIdx = r.data.findIndex(item => item.OpenID === that.data.openInfo.openid)
          that.setData({
            authorizationFlag: aIdx === -1 
          })
        }
      }else{
        wx.showToast({
          title: r.message,
          icon:'none'
        })
      }
    })
  },
  // 获取资产处置服务状态
  getPreStatus(){
    const _this = this
    wx.request({
      url: app.data.api_url2+"v/vip_mini_info/",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        user_id: this.data.FjLoginLog.UID,
      },
      method: 'POST',
      success: function(res) {
        if (res.statusCode == 200) {
          _this.setData({
            preStatus: res.data.data.status
          })
        }
      }
    })
  }
  
})